package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/getp")
    public String getp(@RequestBody MultipartFile file) throws IOException {
        if (file!=null){
            file.transferTo(new File("C:\\Users\\HP\\Desktop\\123.jpg"));
            System.err.println("写入成功");
        }
        return "Ojbk";
    }
    @GetMapping("/hello")
    public List<User> hello() {
        return userService.findAll();
    }

    @PostMapping("/login")
    public SysResult login(@RequestBody User user) {
        System.err.println(user);
//        int a=1/0;
        String token = userService.findUserByUP(user);
        if (StringUtils.hasLength(token)) {
            return SysResult.success(token);
        }else {
            return SysResult.fail();
        }

    }
    @GetMapping("/list")
    public SysResult getlist(PageResult pageResult){
        //携带所有数据返回
        pageResult=userService.findUserByPage(pageResult);
        System.err.println("成功");
        return SysResult.success(pageResult);

    }
    @PutMapping("/status/{id}/{status}")
    public SysResult changestatus(@PathVariable Integer id, @PathVariable Boolean status){

        return SysResult.success("",userService.updateStatus(id,status));
    }
    @PostMapping("/insert")
    public SysResult insert(@RequestBody User user){
       if (userService.insert(user)!=0)
        return SysResult.success();
       else
        return SysResult.fail();
    }
    @DeleteMapping("/delete/{id}")
    public SysResult deletebyid(@PathVariable Integer id){
        if(userService.deletebyid(id)>0){
            return SysResult.success();

        }
        return SysResult.fail();
    }
    @PutMapping("/update/{id}/{phone}/{email}")
    public SysResult update(@PathVariable  Integer id,@PathVariable String phone ,@PathVariable String email){
        User user=new User();
        user.setId(id).setEmail(email).setPhone(phone);
        if(userService.update(user)>0)
            return SysResult.success();
        else
            return SysResult.fail();
    }
}
